package cn.linlane.controller.sys;

import cn.linlane.controller.BaseController;
import cn.linlane.entity.errors.MyError;
import cn.linlane.entity.exception.MyException;
import cn.linlane.entity.sys.SysAuth;
import cn.linlane.entity.sys.SysRole;
import cn.linlane.entity.sys.SysUser;
import cn.linlane.service.sys.SysAuthService;
import cn.linlane.service.sys.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@Api(value = "角色接口说明", description = "角色接口说明")
@RestController
@RequestMapping(value = "/role")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysAuthService sysAuthService;

    @ApiOperation(value = "根据Id查询角色", httpMethod = "GET", response = SysRole.class)
    @GetMapping("/{id}")
    public Object get(@PathVariable Integer id){
        return setSuccessMyMap(sysRoleService.findSysRoleById(id));
    }

    @ApiOperation(value = "添加角色的权限信息", httpMethod = "POST", response = SysUser.class)
    @PostMapping
    public Object post(@RequestParam Set<Integer> authId,
                       @RequestBody SysRole sysRole){
        if ( !sysRoleService.existsByRoleName(sysRole.getRoleName())){
            Set<SysAuth> sysAuths = sysAuthService.findAllSysAuthsById(authId);
            sysRole.setSysAuths(sysAuths);
            return setSuccessMyMap(sysRoleService.add(sysRole));
        }else {
            throw new MyException(MyError.ROLE_EXISTS);
        }

    }

    @ApiOperation(value = "修改角色的权限信息", httpMethod = "PUT", response = SysRole.class)
    @PutMapping
    public Object put(@RequestParam Set<Integer> authId,
                      @RequestBody SysRole sysRole){
        if ( sysRoleService.existsByRoleName(sysRole.getRoleName())){
            Set<SysAuth> sysAuths = sysAuthService.findAllSysAuthsById(authId);
            sysRole.setSysAuths(sysAuths);
            return setSuccessMyMap(sysRoleService.update(sysRole));
        } else {
            throw new MyException(MyError.ROLE_NOT_EXISTS);
        }

    }


    @ApiOperation(value = "根据中英文角色名称查询所有角色", httpMethod = "GET", response = SysUser.class)
    @GetMapping("/list")
    public Object getDeparts(@ApiParam(value = "中英文角色名") @RequestParam(required = false, defaultValue = "") String searchKey,
                             @ApiParam(value = "当前页数") @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                             @ApiParam(value = "分页数量") @RequestParam(required = false, defaultValue = "30") Integer pageSize) {
        return setSuccessMyMap(sysRoleService.getRoles(searchKey, pageNum, pageSize));
    }

    @ApiOperation(value = "更改用角色禁用状态", httpMethod = "PUT", response = SysRole.class)
    @PutMapping("/status")
    public Object putState(@RequestParam Integer id, @RequestParam boolean isEnabled) {
        SysRole sysRole = sysRoleService.queryById(id);
        sysRole.setEnabled(isEnabled);
        return setSuccessMyMap(sysRoleService.update(sysRole));
    }

    @ApiOperation(value = "删除角色", httpMethod = "DELETE", response = void.class)
    @DeleteMapping("/{id}")
    public Object delete(@PathVariable Integer id) {
        sysRoleService.delete(id);
        return setSuccessMyMap(null);
    }
}
